﻿using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.IService;
using Pay.Service.Cache;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Service {
    public class WXVerificationCodeService : ServiceBase, IWXVerificationCodeService, IDisposable {
        private IUnitOfWork _iUnitOfWork;
        private ISoupTaskService _iSoupTaskService;
        public WXVerificationCodeService(IUnitOfWork iUnitOfWork, ISoupTaskService iSoupTaskService) : base(iUnitOfWork) {
            _iUnitOfWork = iUnitOfWork;
            _iSoupTaskService = iSoupTaskService;
        }

        /// <summary>
        /// 获得微信验证码列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResult<wx_verification_code_dto> GetWXVerificationCodes(WXVerificationCodeQuery query) {
            var filter = PredicateBuilder.True<WXVerificationCode>();
            if (!string.IsNullOrEmpty(query.verification_code)) {
                filter = filter.And(c => c.VerificationCode == query.verification_code);
            }
            if (!string.IsNullOrEmpty(query.begin_time)) {
                DateTime time = DateTime.Parse(query.begin_time);
                filter = filter.And(c => c.CreateTime >= time);
            }
            if (!string.IsNullOrEmpty(query.end_time)) {
                DateTime time = DateTime.Parse(query.end_time).AddDays(1);
                filter = filter.And(c => c.CreateTime <= time);
            }
            var result = context.WXVerificationCode.FromCachePaged<WXVerificationCode, wx_verification_code_dto>(query.page, query.size, filter, orderBy => orderBy.OrderByDescending(o => o.CreateTime));
            return result;
        }

        /// <summary>
        /// 获得微信验证码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public wx_verification_code_dto GetWXVerificationCode(long id) {
            return context.WXVerificationCode.Find(id).ToDtoObject<wx_verification_code_dto>();
        }

        /// <summary>
        /// 获取/新增微信验证码
        /// </summary>
        /// <param name="wxOpenId"></param>
        /// <returns></returns>
        public WXVerificationCode GetOrAddWXCode(string wxOpenId) {
            WXVerificationCode wxVerificationCode = context.WXVerificationCode.Where(o => o.WXOpenId == wxOpenId).FirstOrDefault();
            if (wxVerificationCode != null)
                return wxVerificationCode;

            //若该微信用户没有添加验证码过则新增
            WXVerificationCode model = new WXVerificationCode() { VerificationCode = "", WXOpenId = wxOpenId, CreateTime = DateTime.Now };
            var result = context.WXVerificationCode.Add(model);
            _iUnitOfWork.Commit();
            string code = RandomCodeHelp.createCode(result.Id);
            model.VerificationCode = code;
            _iUnitOfWork.Commit();
            return result;
        }

    }
}
